<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="35%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
            <el-form-item label="休药天数" prop="days">
                <el-input type="number" v-model="ruleForm.days" class="w100p" placeholder="请输入休药天数">
                    <template slot="append">天</template>
                </el-input>
            </el-form-item>
            <el-form-item label="选择药品" prop="materialNo">
                <el-select v-model="ruleForm.materialNo" class="w100p" clearable filterable placeholder="请选择物料"
                    @change="querymaterialName">
                    <!-- 动保：4165464165465146516 -->
                    <!-- 药品：1077222993386450944 -->
                    <el-option
                        v-if="item.materialTypeNo == '1077222993386450944' || item.materialTypeNo == '4165464165465146516'"
                        v-for="item, index in materialList" :key="index" :label="item.materialName"
                        :value="item.materialNo"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import drugWithdrawalPeriod from '@/api/healthManagement/drugWithdrawalPeriod.js';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        materialList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        let validatorNumber = function (rule, value, callback) {
            if (value > 0 && value <= 999) {
                callback()
            } else {
                callback(new Error('请输入大于0小于999的数值！'))
            }
        }
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                materialNo: [
                    { required: true, message: '请选择药品', trigger: 'change' }
                ],
                days: [
                    { required: true, validator: validatorNumber, trigger: 'change' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = drugWithdrawalPeriod.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = drugWithdrawalPeriod.edit
                this.addEditName = '编辑'
            }
        },
        querymaterialName(no) {
            let result = this.materialList.find(obj => obj.materialNo == no);
            result.materialNo && this.$set(this.ruleForm, 'materialName', result.materialName)
            console.log(this.ruleForm)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>

<style lang="scss" scoped></style>